package com.wwwarehouse.usercenter.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.response.InviteInfoResponseBean;

import java.util.List;

import static com.wwwarehouse.usercenter.R.drawable.select_permission_pic;

/**
 * 订单导入设置主页Adapter
 * Created by ShineF on 2017/8/11 0011.
 */

public class SelectDistributionRecycleAdapter extends RecyclerView.Adapter<SelectDistributionRecycleAdapter.ImportOrdersPagersHolder> {

    private Context mContext = null;
    private List<InviteInfoResponseBean.InviteDetailsBean> mList = null;
    private int mHeight = 0;
    private OnItemClickListener mListener = null;

    public SelectDistributionRecycleAdapter(Context context, List<InviteInfoResponseBean.InviteDetailsBean> list, int height) {
        this.mContext = context;
        this.mList = list;
        this.mHeight = height;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public ImportOrdersPagersHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ImportOrdersPagersHolder(
                View.inflate(mContext, R.layout.adapter_select_distribution_recycle, null));
    }

    @Override
    public void onBindViewHolder(ImportOrdersPagersHolder holder, final int position) {
        final InviteInfoResponseBean.InviteDetailsBean listBean = mList.get(position);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mRlItem.getLayoutParams();
        params.height = (mHeight - ConvertUtils.dip2px(mContext, 20)) / 3;
        if (listBean.isSelect()) {
            holder.mRlItemSelect.setVisibility(View.VISIBLE);
        } else {
            holder.mRlItemSelect.setVisibility(View.GONE);
        }
        if (null != listBean.getFaceUrl()) {
            BaseApplication.getApplicationInstance().displayImg(listBean.getFaceUrl(), holder.mIvHeadPic);
        } else {
            holder.mIvHeadPic.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.select_permission_pic));
        }
        if (null != listBean.getInviteeName()) {
            holder.mTvName.setText(listBean.getInviteeName());
        }
        if (null != listBean.getInviteeMobile()) {
            holder.mTvPhoneNumber.setText(listBean.getInviteeMobile());
        }
        holder.mRlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listBean.setSelect(!listBean.isSelect());
                if (mListener != null) {
                    mListener.ItemClickListener(position, v, listBean);
                }
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public interface OnItemClickListener {
        void ItemClickListener(int position, View view, InviteInfoResponseBean.InviteDetailsBean data);
    }

    class ImportOrdersPagersHolder extends RecyclerView.ViewHolder {

        RelativeLayout mRlItem;
        RelativeLayout mRlItemSelect;
        CircleImageView mIvHeadPic;
        TextView mTvName;
        TextView mTvPhoneNumber;

        ImportOrdersPagersHolder(View itemView) {
            super(itemView);
            mRlItem = (RelativeLayout) itemView.findViewById(R.id.rl_item);
            mRlItemSelect = (RelativeLayout) itemView.findViewById(R.id.rl_item_select);
            mIvHeadPic = (CircleImageView) itemView.findViewById(R.id.iv_permission_user_pic);
            mTvName = (TextView) itemView.findViewById(R.id.tv_permission_user_name);
            mTvPhoneNumber = (TextView) itemView.findViewById(R.id.tv_permission_user_number);
        }
    }
}
